﻿Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.IO
Imports SubSonic
Imports ClubStarterKit.Data

Public Class ImageUtils

    Public Enum ImageLocation As Integer
        Database = 0
        FileSystem = 1
    End Enum

    Public Shared Function GetLocation() As ImageLocation
        If ConfigurationManager.AppSettings("StoreImagesOnFile").ToString() = "True" Then
            Return ImageUtils.ImageLocation.FileSystem
        Else
            Return ImageUtils.ImageLocation.Database
        End If
    End Function

    Public Shared Function uploadImage(ByVal title As String, ByVal albumid As Integer, ByVal data As IO.Stream, Optional ByVal path As String = "") As Integer
        Dim origImageData(CInt(data.Length - 1)) As Byte
        data.Read(origImageData, 0, CInt(data.Length))

        Dim imgStore As ImageLocation = GetLocation()

        If imgStore = ImageLocation.Database Then
            Images.Insert(title, MakeThumb(origImageData, 350), MakeThumb(origImageData, 69, 69), origImageData, albumid, "")

            Dim imgs As New Images(Images.Columns.Origimage, origImageData)
            If imgs.IsLoaded = True Then
                Return imgs.Id
            Else
                Return 0
            End If
        ElseIf imgStore = ImageLocation.FileSystem Then
            If Not Directory.Exists(path) Then
                Directory.CreateDirectory(path)
            End If

            Dim filename As String = Now.Year & Now.DayOfYear & Now.Hour & Now.Minute & Now.Second & Now.Millisecond & ".jpg"

            Dim thumbLargeImage As Byte() = MakeThumb(origImageData, 350)
            Dim thumbSmallImage As Byte() = MakeThumb(origImageData, 69, 69)

            Dim newImageFile As FileStream = New FileStream(path & filename, FileMode.Create)
            newImageFile.Write(origImageData, 0, origImageData.Length)
            newImageFile.Close()

            Dim newThumbFile As New FileStream(path & "large" & filename, FileMode.Create)
            newThumbFile.Write(thumbLargeImage, 0, thumbLargeImage.Length)
            newThumbFile.Close()

            Dim newThumb As New FileStream(path & "tmb" & filename, FileMode.Create)
            newThumb.Write(thumbSmallImage, 0, thumbSmallImage.Length)
            newThumb.Close()

            Image.Insert(title, filename, albumid, "")

            Dim newimg As New Image(Image.Columns.ImageFileName, filename)
            If newimg.IsLoaded = True Then
                Return newimg.Id
            Else
                Return 0
            End If
        End If

    End Function

    

    Public Shared Sub uploadTempImage(ByVal title As String, ByVal albumid As Integer, ByVal data As IO.Stream, Optional ByVal path As String = "")
        Dim origImageData(CInt(data.Length - 1)) As Byte
        data.Read(origImageData, 0, CInt(data.Length))

        Dim imgStore As ImageLocation = GetLocation()

        If imgStore = ImageLocation.Database Then

            TempImagesDB.Insert(origImageData, albumid, title, MakeThumb(origImageData, 69, 69))

        ElseIf imgStore = ImageLocation.FileSystem Then
            If Not Directory.Exists(path) Then
                Directory.CreateDirectory(path)
            End If

            Dim filename As String = Now.Year & Now.DayOfYear & Now.Hour & Now.Minute & Now.Second & Now.Millisecond & ".jpg"


            Dim newImageFile As FileStream = New FileStream(path & filename, FileMode.Create)
            newImageFile.Write(origImageData, 0, origImageData.Length)
            newImageFile.Close()

            Dim thumbSmallImage As Byte() = MakeThumb(origImageData, 69, 69)
            Dim newThumb As New FileStream(path & "tmb" & filename, FileMode.Create)
            newThumb.Write(thumbSmallImage, 0, thumbSmallImage.Length)
            newThumb.Close()

            TempImagesFile.Insert(filename, albumid, title)

        End If

    End Sub



    Const sizeThumb As Integer = 69

    Public Shared Function MakeThumb(ByVal fullsize As Byte()) As Byte()
        Dim iOriginal, iThumb As System.Drawing.Image
        Dim targetH, targetW As Integer

        iOriginal = System.Drawing.Image.FromStream(New IO.MemoryStream(fullsize))

        If (iOriginal.Height > iOriginal.Width) Then
            targetH = sizeThumb
            targetW = CInt(iOriginal.Width * (sizeThumb / iOriginal.Height))
        Else
            targetW = sizeThumb
            targetH = CInt(iOriginal.Height * (sizeThumb / iOriginal.Width))
        End If
        iThumb = iOriginal.GetThumbnailImage(targetW, targetH, Nothing, System.IntPtr.Zero)
        Dim m As New IO.MemoryStream()
        iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
        Return m.GetBuffer()
    End Function


    Public Shared Function MakeThumb(ByVal fullsize As Byte(), ByVal newwidth As Integer, ByVal newheight As Integer) As Byte()
        Dim iOriginal, iThumb As System.Drawing.Image
        Dim scaleH, scaleW As Double
        Dim srcRect As Drawing.Rectangle

        iOriginal = System.Drawing.Image.FromStream(New IO.MemoryStream(fullsize))

        scaleH = iOriginal.Height / newheight
        scaleW = iOriginal.Width / newwidth
        If scaleH = scaleW Then
            srcRect.Width = iOriginal.Width
            srcRect.Height = iOriginal.Height
            srcRect.X = 0
            srcRect.Y = 0
        ElseIf (scaleH) > (scaleW) Then
            srcRect.Width = iOriginal.Width
            srcRect.Height = CInt(newheight * scaleW)
            srcRect.X = 0
            srcRect.Y = CInt((iOriginal.Height - srcRect.Height) / 2)
        Else
            srcRect.Width = CInt(newwidth * scaleH)
            srcRect.Height = iOriginal.Height
            srcRect.X = CInt((iOriginal.Width - srcRect.Width) / 2)
            srcRect.Y = 0
        End If

        iThumb = New System.Drawing.Bitmap(newwidth, newheight)
        Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(iThumb)
        g.DrawImage(iOriginal, New Drawing.Rectangle(0, 0, newwidth, newheight), srcRect, Drawing.GraphicsUnit.Pixel)

        Dim m As New IO.MemoryStream()
        iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
        Return m.GetBuffer()
    End Function

    Public Shared Function MakeThumb(ByVal fullsize As Byte(), ByVal maxwidth As Integer) As Byte()
        Dim iOriginal, iThumb As System.Drawing.Image
        Dim scale As Double

        iOriginal = System.Drawing.Image.FromStream(New IO.MemoryStream(fullsize))

        If iOriginal.Width > maxwidth Then

            scale = iOriginal.Width / maxwidth
            Dim newheight As Integer = CInt(iOriginal.Height / scale)

            iThumb = New System.Drawing.Bitmap(iOriginal, maxwidth, newheight)
            Dim m As New IO.MemoryStream()
            iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
            Return m.GetBuffer()
        Else
            Return fullsize
        End If
    End Function

End Class